package cn.bugDance.domain.strategy.service.rule.tree.impl;


import cn.bugDance.domain.strategy.model.entity.RuleConfigEntity;
import cn.bugDance.domain.strategy.model.entity.RuleTreeDetermineEntity;
import cn.bugDance.domain.strategy.model.entity.RuleTreeFactorEntity;
import cn.bugDance.domain.strategy.model.valobj.DetermineTypeVO;
import cn.bugDance.domain.strategy.model.valobj.StrategyConstant;
import cn.bugDance.domain.strategy.repository.IStrategyRepository;
import cn.bugDance.domain.strategy.service.rule.tree.AbstractRuleTree;
import cn.bugDance.domain.strategy.service.rule.tree.factory.RuleTreeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component(StrategyConstant.StrategyRule.RULE_LUCK_AWARD)
@Slf4j
public class RuleLuckAwardTree extends AbstractRuleTree {
    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public RuleTreeDetermineEntity action(RuleTreeFactorEntity ruleTreeFactorEntity) {
        log.info("【策略领域层-RuleLuckAwardTree】: 规则规则树引擎节点-兜底幸运奖品锁执行, strategyId: {}, awardId: {}", ruleTreeFactorEntity.getStrategyId(), ruleTreeFactorEntity.getAwardId());

        Long strategyId = ruleTreeFactorEntity.getStrategyId();
        Integer awardId = ruleTreeFactorEntity.getAwardId();
        List<String> ruleModels = ruleTreeFactorEntity.getRuleModels();

        if(!ruleModels.contains(StrategyConstant.StrategyRule.RULE_LUCK_AWARD)){
            RuleTreeDetermineEntity ruleTreeDetermineEntity = new RuleTreeDetermineEntity();
            ruleTreeDetermineEntity.setDetermineTypeVO(DetermineTypeVO.ALLOW);
            ruleTreeDetermineEntity.setAwardId(awardId);
            return ruleTreeDetermineEntity;
        }

        RuleConfigEntity ruleConfigEntity = strategyRepository.queryRuleConfigRuleAward(strategyId, awardId, StrategyConstant.StrategyRule.RULE_LUCK_AWARD);
        Integer luckAwardId = Integer.valueOf(ruleConfigEntity.getConfig());
        RuleTreeDetermineEntity ruleTreeDetermineEntity = new RuleTreeDetermineEntity();
        ruleTreeDetermineEntity.setDetermineTypeVO(DetermineTypeVO.TAKE_OVER);
        ruleTreeDetermineEntity.setAwardId(luckAwardId);
        return ruleTreeDetermineEntity;
    }
}
